package com.example.factory.responsibility.chain;

import com.example.factory.responsibility.Context;
import com.example.factory.responsibility.ResourceWrapper;
import com.example.factory.responsibility.slot.AbstractLinkedProcessorSlot;

public class DefaultProcessSlotChain extends ProcessChain{
    AbstractLinkedProcessorSlot<?> first = new AbstractLinkedProcessorSlot<Object>() {
        @Override
        public void entry(Context context, ResourceWrapper wrapper, Object param) {
            super.fireEntry(context, wrapper, param);
        }

        @Override
        public void exit(Context context, ResourceWrapper wrapper, Object param) {
            super.fireExit(context,wrapper,param);
        }
    };
    AbstractLinkedProcessorSlot<?> end = first;

    @Override
    public void addFirst(AbstractLinkedProcessorSlot<?> slot) {
        slot.setNext(first.getNext());
        first.setNext(slot);
        if (end == first) {
            end = slot;
        }
    }

    @Override
    public void addLast(AbstractLinkedProcessorSlot<?> slot) {
        end.setNext(slot);
        end = slot;
    }

    @Override
    public void entry(Context context, ResourceWrapper wrapper, Object param) {
        first.transFormEntry(context, wrapper, param);
    }

    @Override
    public void exit(Context context, ResourceWrapper wrapper, Object param) {
        first.exit(context, wrapper, param);
    }
    @Override
    public AbstractLinkedProcessorSlot<?> getNext() {
        return first.getNext();
    }

    @Override
    public void setNext(AbstractLinkedProcessorSlot<?> next){
        addLast(next);
    }
}
